package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SearchAdapter;
import flc.ast.databinding.ActivitySearchBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.bean.StkResBean;

public class SearchActivity extends BaseAc<ActivitySearchBinding> {

    public static String key;
    private List<StkResBean> listShow = new ArrayList<>();
    private SearchAdapter searchAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSearchBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);

        mDataBinding.rvSearchList.setLayoutManager(new LinearLayoutManager(mContext));
        searchAdapter = new SearchAdapter();
        mDataBinding.rvSearchList.setAdapter(searchAdapter);
        searchAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_text));
                    return;
                }
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        mDataBinding.etSearch.setText(key);
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.SEARCH_DATA + key, StkResApi.createParamMap(0, 99), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        searchAdapter.setList(result);
                        mDataBinding.rvSearchList.setVisibility(View.VISIBLE);
                    } else {
                        mDataBinding.rvSearchList.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

}